package com.dreamcode.anfeedreader.collectors;

import java.util.LinkedList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

import com.dreamcode.anfeedreader.database.DbHelper;
import com.dreamcode.anfeedreader.modelClass.AccountObject;

public class SettingsDAOimpl implements SettingsDAO{
	
	private static SettingsDAOimpl settingsDAOimpl = null;
	private DbHelper dbHelper = null;
	private AccountObject accountObject = null;
	
	public static SettingsDAOimpl getInstance(){
		if(settingsDAOimpl==null){
			settingsDAOimpl = new SettingsDAOimpl();
		}		
		return settingsDAOimpl;
	}

	@Override
	public void registerAccount(Context ctx, String type, String token, String tokenSecret) {
		dbHelper = DbHelper.getInstance(ctx.getApplicationContext());
		ContentValues contentValues = new ContentValues();
		contentValues.put("type", type);
		contentValues.put("accesstoken", token);
		contentValues.put("tokensecret", tokenSecret);
		dbHelper.getDb().insert(dbHelper.DB_TABLE_ACCOUNTS, null,
				contentValues);
				
	}

	@Override
	public boolean checkAccountExistence(Context ctx, String type) {
		dbHelper = DbHelper.getInstance(ctx.getApplicationContext());
		Cursor c = dbHelper.getDb().query(DbHelper.DB_TABLE_ACCOUNTS, null, "type='"+type+"'", null, null, null, null);
		boolean exist = false;
		if(c!=null){
			c.moveToFirst();
			exist = (c.getCount()>0)?true:false;
			c.close();
			return exist;				
		}else{
			c.close();
			return false;
		}		
	}

	@Override
	public void deleteAccount(Context ctx, int id) {
		dbHelper = DbHelper.getInstance(ctx.getApplicationContext());
		dbHelper.getDb().delete(dbHelper.DB_TABLE_ACCOUNTS, "_id=" + id, null);		
	}

	@Override
	public AccountObject getAccoutSettings(Context ctx, String type) {
		dbHelper = DbHelper.getInstance(ctx.getApplicationContext());
		Cursor c = dbHelper.getDb().query(DbHelper.DB_TABLE_ACCOUNTS, null, "type='"+type+"'", null, null, null, null);
		boolean exist = false;
		if(c!=null){
			c.moveToFirst();
			accountObject = new AccountObject();
			accountObject.setId(c.getInt(c.getColumnIndex("_id")));
			accountObject.setAccessToken(c.getString(c.getColumnIndex("accesstoken")));
			accountObject.setAccessSecret(c.getString(c.getColumnIndex("tokensecret")));
			c.close();
			return accountObject;
		}
		c.close();
		return null;
	}

	@Override
	public List<AccountObject> getAllAccounts(Context ctx) {
		List<AccountObject> accountObjects = new LinkedList<AccountObject>();
		dbHelper = DbHelper.getInstance(ctx.getApplicationContext());
		Cursor c = dbHelper.getDb().query(DbHelper.DB_TABLE_ACCOUNTS, null, null, null, null, null, null);
		boolean exist = false;
		if(c!=null){
			c.moveToFirst();
			if(c.getCount()>0){
				do{
					accountObject = new AccountObject();
					accountObject.setId(c.getInt(c.getColumnIndex("_id")));
					accountObject.setAccessToken(c.getString(c.getColumnIndex("accesstoken")));
					accountObject.setAccessSecret(c.getString(c.getColumnIndex("tokensecret")));
					accountObjects.add(accountObject);
				}while(c.moveToNext());
			}
			
		}
		return accountObjects;
	}

}
